#include"log.h"
#include"unistd.h"
using namespace c3pom;
using namespace std;
string log::getTimeofDay()
{
	time_t timet=time(NULL);
	string time=string(asctime(localtime(&timet)));
	if(time.find("\n")==time.length()-1)
	{
		time=string(time,0,time.length()-1);
	}
	time=string("[")+time+string("]");
	return time;
}	
int log::load()
{
	info=PortalManager::getInstance()->regPortal("log.info","queue","log","std::string");
	warn=PortalManager::getInstance()->regPortal("log.warn","queue","log","std::string");
	error=PortalManager::getInstance()->regPortal("log.error","queue","log","std::string");
}
int log::stop(vector<string>)
{
}
log::log()
{
	sinfo="(INFO)";
	serror="(ERRO)";
	swarn="(WARN)";
	string filename="log.log";
	f.open(filename.c_str(),ios::out|ios::app);
}	

int log::run(vector<string>)
{
	PortalBlock* pb;
	while(true)
	{
		(*info)>>pb;
		if(pb!=NULL)
		{
			f<<getTimeofDay()+sinfo+pb->getData().castto<string>();
		}
		(*warn)>>pb;
		if(pb!=NULL)
		{
			f<<getTimeofDay()+swarn+pb->getData().castto<string>();
		}
		(*error)>>pb;
		if(pb!=NULL)
		{
			f<<getTimeofDay()+serror+pb->getData().castto<string>();
		}
		sleep(1);
	}
}
